define(
		[ "PDUtilDir/util",  "PDUtilDir/tool", "PDUtilDir/slidebar", "AppPathDir/utils", "Date", "DateCN",
            "css!DateCss", "css!AppPathDir/product/productInfos/views/css/css"],
		function(Util, Tool, Slidebar, Common) {
			function addSupportInit($scope, $compile) {
				return new addSupport($scope, $compile);
			}
			function addSupport($scope, $compile) {
				this.$scope = $scope;
				this.$compile = $compile;
			}
			addSupport.fn = addSupport.prototype;
			
			addSupport.fn.init = function() {
				this.entity = {
                    timetable: new Array()
				};
				this.cache = {
                    weekEn: {
                        0: 'sun',
                        1: 'mon',
                        2: 'tue',
                        3: 'wed',
                        4: 'thur',
                        5: 'fri',
                        6: 'sat'
                    }
				};
				this.bind = {
                    timetable: {
                        costPrice: this.$scope.entity.product.costPrice,
                        salePriceAdult: this.$scope.entity.product.salePriceAdult,
                        costPriceChild: this.$scope.entity.product.costPriceChild,
                        salePriceChild: this.$scope.entity.product.salePriceChild,
                        costPriceOther: this.$scope.entity.product.costPriceOther,
                        salePriceOther: this.$scope.entity.product.salePriceOther
					},
                    isOk: true
				};
			};
			
			addSupport.fn.digest = function() {
				this.$scope.$digest();
			};
			addSupport.fn.run = function() {
				var _this = this;
				_this.init();
				this.slidebars = Slidebar({
					size : 'lg',
					url : getServer()
							+ "/static/app/product/productInfos/views/addTimetable.html",
					$scope : this.$scope,
					$compile : this.$compile,
					afterLoad : function($Panel) {
						_this.initialization();
					}
				});
			};
			addSupport.fn.initialization = function() {
				this.timetableInit();
			};

			addSupport.fn.timetableInit = function() {
				var _this = this;

				$("#startDate").datetimepicker({
					// 设置使用语言：cn是自定义的中文版本，还可以扩展其他语言版本
					language : "cn",
					// 输出格式化
					format : 'yyyy-mm-dd',
					// 直接选择‘今天’
					todayBtn : true,
					// 设置最精确的时间选择视图
					minView : 'month',
					// 高亮当天日期
					todayHighlight : true,
					// 选择完毕后自动关闭
					autoclose : true
				});
				$("#endDate").datetimepicker({
					// 设置使用语言：cn是自定义的中文版本，还可以扩展其他语言版本
					language : "cn",
					// 输出格式化
					format : 'yyyy-mm-dd',
					// 直接选择‘今天’
					todayBtn : true,
					// 设置最精确的时间选择视图
					minView : 'month',
					// 高亮当天日期
					todayHighlight : true,
					// 选择完毕后自动关闭
					autoclose : true
				});
			};

            addSupport.fn.addTimetable = function() {
                var daysArray = new Array(),
                    startDate = this.bind.timetable.startDate,
					weeks = this.bind.week,
					dateDay = 0,
					dateStr = undefined,
                    days = Common.getDayByDateRange(startDate, this.bind.timetable.endDate);

                console.info(weeks);
				console.info(days);

                if(weeks){
					for(var i = 0; i <= days; i++){
						dateStr = Common.addDate(startDate, i);
						dateDay = (new Date(dateStr).getDay());

						if(weeks[this.cache.weekEn[dateDay]]){
							daysArray.push($.extend({
								date : dateStr
							}, this.bind.timetable));
						}
					}
				}else{
					for(var i = 0; i <= days; i++){
						daysArray.push($.extend({
							date : Common.addDate(startDate, i)
						}, this.bind.timetable));
					}
				}

                this.entity.timetable = this.entity.timetable.concat(daysArray);
                this.bind.timetable = {
                    costPrice: this.$scope.entity.product.costPrice,
                    salePriceAdult: this.$scope.entity.product.salePriceAdult,
                    costPriceChild: this.$scope.entity.product.costPriceChild,
                    salePriceChild: this.$scope.entity.product.salePriceChild,
                    costPriceOther: this.$scope.entity.product.costPriceOther,
                    salePriceOther: this.$scope.entity.product.salePriceOther
				};
                this.bind.week = undefined;
            };

			addSupport.fn.save = function() {
				var _this = this;
				$.ajax({
					url : getServer() + "/flea/hclv/product/saveTimeTable.htmls",
					data : {
						'model' : JSON.stringify({
							productId: _this.$scope.entity.product.id,
							timeTables: _this.entity.timetable
						})
					},
					success : function(data) {
                        if (data.status != 200) {
                            return;
                        }
						_this.$scope.grid.refresh();
						Util.alert('成功');
						_this.slidebars.close();
					}
				});
			};
			return addSupportInit;
		});